package com.huadao.cart_ccs.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.common.RockToast;
import com.huadao.cart_ccs.common.dialog.DialogFragmentHelper;
import com.huadao.cart_ccs.exception.ActivityNotAttachedException;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment extends Fragment implements BaseView {
    public abstract int getContentViewId();

    protected abstract void initAllMembersView(Bundle savedInstanceState);

    protected Context mContext;
    protected View mRootView;
//    protected DialogUtil dialogUtil;
    protected Toolbar toolbar;
    protected Unbinder unbinder;

    private View back;
    private TextView tv_title;

    protected BasePresenter basePresenter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(getContentViewId(), container, false);
        this.mContext = getActivity();
//        dialogUtil=new DialogUtil(mContext);
        unbinder=ButterKnife.bind(this,mRootView);

        initActionBar();
        initAllMembersView(savedInstanceState);
        basePresenter=getPresenter();
        return mRootView;
    }

    protected void initActionBar() {
        toolbar = mRootView.findViewById(R.id.toolbar);
        if (toolbar != null) {
            back=mRootView.findViewById(R.id.back);
            tv_title=mRootView.findViewById(R.id.title);
            if(back!=null) {
                back.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onBack();
                    }
                });
            }
            ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
            ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
            if (actionBar != null) {
                actionBar.setDisplayShowTitleEnabled(false);
            }
        }
    }

    protected void onBack() {}

    protected void setTitle(String title) {
        if(tv_title!=null) {
            tv_title.setText(title);
        }
    }

    abstract protected BasePresenter getPresenter();

    @Override
    public void showLoading(String msg) {
        checkActivityAttached();
        ((BaseActivity) mContext).showLoading(msg);
    }

    @Override
    public void hideLoading() {
        checkActivityAttached();
        ((BaseActivity) mContext).hideLoading();
    }

    @Override
    public void showToast(String msg) {
        checkActivityAttached();
        RockToast.showToastSafe(msg);
    }

    @Override
    public void showDialogMessage(String title,String msg) {
        if(TextUtils.isEmpty(title)){
            title="";
        }
        DialogFragmentHelper.showMessageDialog(getActivity(),title,msg);
    }

    /**
     * 检查activity连接情况
     */
    public void checkActivityAttached() {
        if (getActivity() == null) {
            throw new ActivityNotAttachedException();
        }
    }

    @Override
    public void onDestroyView() {
        if(basePresenter!=null){
            basePresenter.detachView();
        }
        unbinder.unbind();
        super.onDestroyView();
    }
}
